package com.audaque.springboot.foshanupload.web.lic.service.impl;

import com.audaque.springboot.foshanupload.core.util.ResourceUtil;
import com.audaque.springboot.foshanupload.web.lic.common.license.bo.LicenseCheckModel;
import com.audaque.springboot.foshanupload.web.lic.common.utils.os.AbstractServerInfo;
import com.audaque.springboot.foshanupload.web.lic.common.utils.os.LinuxServerInfo;
import com.audaque.springboot.foshanupload.web.lic.common.utils.os.WindowsServerInfo;
import com.audaque.springboot.foshanupload.web.lic.creator.LicenseCreator;
import com.audaque.springboot.foshanupload.web.lic.creator.LicenseCreatorParam;
import com.audaque.springboot.foshanupload.web.lic.service.LicenseCreatorService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.time.ZoneId;
import java.util.Date;

/**
 * @author zgb
 * @desc ...
 * @date 2022-10-24 18:01:45
 */
@Slf4j
@Service
public class LicenseCreatorServiceImpl implements LicenseCreatorService {


    /**
     * 证书生成路径
     */
    @Value("${licenseServer.licensePath}")
    private String licensePath;
    @Value("${licenseServer.privateKeysStorePath}")
    private String privateKeysStorePath;

    @Value("${licenseServer.subject}")
    private String subject;

    @Value("${licenseServer.privateAlias}")
    private String privateAlias;

    @Value("${licenseServer.keyPass}")
    private String keyPass;

    @Value("${licenseServer.storePass}")
    private String storePass;


    public LicenseCheckModel getLicenseCheckModel(String osName) {
        //操作系统类型
        if (StringUtils.isBlank(osName)) {
            osName = System.getProperty("os.name");
        }
        osName = osName.toLowerCase();
        AbstractServerInfo abstractServerInfo;
        //根据不同操作系统类型选择不同的数据获取方法
        if (osName.startsWith("windows")) {
            abstractServerInfo = new WindowsServerInfo();
        } else if (osName.startsWith("linux")) {
            abstractServerInfo = new LinuxServerInfo();
        } else {//其他服务器类型
            abstractServerInfo = new LinuxServerInfo();
        }
        LicenseCheckModel licenseCheckModel = LicenseCheckModel.installServerInfo(abstractServerInfo);
        return licenseCheckModel;
    }


    public boolean generateLicense() throws Exception {
        LicenseCreatorParam param = new LicenseCreatorParam();

        param.setSubject(subject);
        param.setPrivateAlias(privateAlias);
        param.setKeyPass(keyPass);
        param.setStorePass(storePass);

        LocalDate now = LocalDate.now();
        LocalDate plus = now.plusYears(100);

        Date date1 = Date.from(now.atStartOfDay().atZone(ZoneId.systemDefault()).toInstant());
        Date date2 = Date.from(plus.atStartOfDay().atZone(ZoneId.systemDefault()).toInstant());

        param.setIssuedTime(date1);
        param.setExpiryTime(date2);
        LicenseCheckModel licenseCheckModel = this.getLicenseCheckModel(null);
        param.setLicenseCheckModel(licenseCheckModel);


        param.setLicensePath(licensePath);

        String privateKeysStorePath1 = ResourceUtil.getPathByHutoolResourceUtil(privateKeysStorePath);
        param.setPrivateKeysStorePath(privateKeysStorePath1);


        LicenseCreator licenseCreator = new LicenseCreator(param);
        boolean result = licenseCreator.generateLicense();
        return result;
    }


}
